package co.com.smart.eagle.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.InvocationException;
import com.google.gwt.user.client.rpc.StatusCodeException;

public abstract class EagleCallBack<T> implements AsyncCallback<ParametroHandler<T>> {

	private boolean mostrarMensaje = false; 

	public interface CallBackError
	{
		void callBack();
	}	

	private CallBackError callBackError;

	public EagleCallBack(boolean ocultarMensaje,CallBackError callBack )
	{
		this(ocultarMensaje);		
		callBackError = callBack;
	}	
	public EagleCallBack(CallBackError callBack )
	{		
		callBackError = callBack;
	} 
	public EagleCallBack(boolean ocultarMensaje)
	{		
		mostrarMensaje = ocultarMensaje;				
	}	
	
	public EagleCallBack(){}
	
	@Override
	public void onFailure(Throwable th) {
		if (th instanceof InvocationException && ! (th instanceof StatusCodeException) && 
				(((InvocationException) th).getMessage().contains("login")))
		{			
			Window.Location.reload();	
			
			Window.alert("Error th"+ th.getMessage());
		}
		else
		{		
			if (callBackError != null)
			{
				callBackError.callBack();
			}
			Window.alert("Error "+ th.getMessage());
			Window.Location.reload(); 	
			th.printStackTrace();
		}
	}

	@Override
	public void onSuccess(ParametroHandler<T> response) {
		if (response != null)
		{
				try
				{
					success(response.getParametroRetorno());
				}
				catch(Throwable e)
				{
					e.printStackTrace();
					Window.alert("Error "+ e.getMessage());
				}
				finally
				{
				}

		}
		else
		{
			if (callBackError != null)
			{
				callBackError.callBack();
			}
		}
	}
	
	public abstract void success(T result);	

}
